package pl.edu.agh.xls2html.htmlelements;

import static org.junit.Assert.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.junit.Before;
import org.junit.Test;

public class CellTest {
	private HSSFWorkbook wb;	
	private HSSFSheet sheet;
	private HSSFRow row;		
	private HSSFCell cell;
	private HSSFCell cell2;
	
	private Document doc;
	private Cell testcell;

	@Before
	public void setUp() throws Exception {
		
		wb = new HSSFWorkbook();
		sheet =  wb.createSheet();
		row = sheet.createRow(0);
		cell = row.createCell(0);
		cell2 = row.createCell(1);
		doc = new Document(wb);		
	}

	@Test
	public void testNumericDynamicHTML() {
		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(123);
		testcell = new Cell(cell,wb,doc);
		
		assertEquals("\t\t<td class=\"A1\" ><input id=\"A1\" value=\"123.0\" onchange=\"evaluateAllFormula()\" /></td>\n", testcell.getHTML());
	}
	
	@Test
	public void testNumericStaticHTML() {
		doc.setStaticMode();
		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(123);
		testcell = new Cell(cell,wb,doc);
		
		assertEquals("\t\t<td class=\"A1\" ><div>123.0</div></td>\n", testcell.getHTML());
	}
	
	@Test
	public void testStringStaticHTML() {
		doc.setStaticMode();
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		HSSFRichTextString str = new HSSFRichTextString("test");
		cell.setCellValue(str);
		testcell = new Cell(cell,wb,doc);		
		assertEquals("\t\t<td class=\"A1\" ><div>test</div></td>\n", testcell.getHTML());
		
	}
	
	@Test
	public void testStringDynamicHTML() {		
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		HSSFRichTextString str = new HSSFRichTextString("test");
		cell.setCellValue(str);
		testcell = new Cell(cell,wb,doc);		
		assertEquals("\t\t<td class=\"A1\" ><input id=\"A1\" value=\"test\" /></td>\n", testcell.getHTML());		
	}
	
	@Test
	public void testBlankDynamicHTML() {		
		cell.setCellType(HSSFCell.CELL_TYPE_BLANK);		
		testcell = new Cell(cell,wb,doc);
		assertEquals("\t\t<td class=\"A1\" ><input id=\"A1\" value=\"\" /></td>\n", testcell.getHTML());		
	}
	
	@Test
	public void testBlankStaticHTML() {
		doc.setStaticMode();
		cell.setCellType(HSSFCell.CELL_TYPE_BLANK);		
		testcell = new Cell(cell,wb,doc);
		assertEquals("\t\t<td class=\"A1\" ><div>&nbsp;</div></td>\n", testcell.getHTML());		
	}
	
	@Test
	public void testFormulaDynamicHTML() {		
		cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
		cell.setCellFormula("A1+A1");
		testcell = new Cell(cell,wb,doc);
		assertEquals("\t\t<td class=\"A1\" >"+
				"<input id=\"A1_Hidden\" value=\"document.getElementById('A1').value="+
				"(parseFloat(document.getElementById('A1').value)+parseFloat(document.getElementById('A1').value))"+
				"\" type=\"hidden\" /><input id=\"A1\" value=\"0.0\" readonly /></td>\n", testcell.getHTML());		
	}
	
	@Test
	public void testFormulaStaticHTML() {
		doc.setStaticMode();
		cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
		cell.setCellFormula("A1+A1");
		testcell = new Cell(cell,wb,doc);
		assertEquals("\t\t<td class=\"A1\" ><div>0.0</div></td>\n", testcell.getHTML());		
	}
	
	@Test
	public void testNoBordersCSS() {
		doc.setStaticMode();
		HSSFRichTextString str = new HSSFRichTextString("test");
		cell.setCellValue(str);
		testcell = new Cell(cell,wb,doc);
		assertEquals(".A1{\n"+
			"\twidth:64px;\n"+
			"\theight:17px;\n"+
			"\tbackground-color: #FFFFFF;\n"+
			"\tborder-top: none  ;\n"+
			"\tborder-left: none  ;\n"+
			"\tborder-right: none  ;\n"+
			"\tborder-bottom: none  ;\n"+
			"\ttext-align: left;\n"+
			"}\n"+
			"#A1{\n"+
			"\tbackground-color: #FFFFFF;\n"+
			"}\n"+
			"td.A1 div{	width:64px;\n"+
			"\theight:17px;\n"+
			"}\n", testcell.getCSS());		
	}
	
	@Test
	public void testMergedCSS() {
		doc.setStaticMode();
		HSSFRichTextString str = new HSSFRichTextString("test");
		cell.setCellValue(str);
		str = new HSSFRichTextString("test2");
		cell2.setCellValue(str);
		CellRangeAddress region = new CellRangeAddress(0,0,0,1);
		sheet.addMergedRegion(region);
		
		testcell = new Cell(cell,wb,doc);
		assertEquals(".A1{\n"+
			"\twidth:128px;\n"+
			"\theight:17px;\n"+
			"\tbackground-color: #FFFFFF;\n"+
			"\tborder-top: none  ;\n"+
			"\tborder-left: none  ;\n"+
			"\tborder-right: none  ;\n"+
			"\tborder-bottom: none  ;\n"+
			"\ttext-align: left;\n"+
			"}\n"+
			"#A1{\n"+
			"\tbackground-color: #FFFFFF;\n"+
			"}\n"+
			"td.A1 div{	width:128px;\n"+
			"\theight:17px;\n"+
			"}\n", testcell.getCSS());
		testcell = new Cell(cell2,wb,doc);
		assertEquals("", testcell.getCSS());	
	}
	
	@Test
	public void testMergedHTML() {
		doc.setStaticMode();
		HSSFRichTextString str = new HSSFRichTextString("test");
		cell.setCellValue(str);
		str = new HSSFRichTextString("test2");
		cell2.setCellValue(str);
		CellRangeAddress region = new CellRangeAddress(0,0,0,1);
		sheet.addMergedRegion(region);
		
		testcell = new Cell(cell,wb,doc);
		assertEquals("		<td class=\"A1\" colspan=\"2\" rowspan=\"1\" ><div>test</div></td>\n", testcell.getHTML());
		testcell = new Cell(cell2,wb,doc);
		assertEquals("", testcell.getHTML());	
	}
	
	


}
